﻿using System.Net.Mail;
using ICalendar.CalendarProperties;

namespace ICalendar
{
    using System;
    using System.Text;

   [Serializable]
   public class Alarm
    {
       
        //Trigger
        private object trigger;
        //SOUND - 
        public CalendarProperties.Action Action { get; set; }
        /// <summary>
        /// The parameter can be specified on properties that specify an alarm trigger with a DURATION value type. The parameter specifies whether the alarm will trigger relative to the start or end of the calendar component. The parameter value START will set the alarm to trigger off the start of the calendar component; the parameter value END will set the alarm to trigger off the end of the calendar component. If the parameter is not specified on an allowable property, then the default is START.
        /// </summary>
        public TriggerRelationship TriggerRelationship { get; set; }
        public int Repeat { get; set; }
        public string Description { get; set; }
        //Title
        public string Summary { get; set; }

        ///<summary>
        /// This property specifies when an alarm will trigger. 
        /// </summary>
        /// <value>The default value type is <see cref="Duration"/>. The value type can be set to a <see cref="DateTime"/> value type, in which case the value MUST specify a UTC formatted DATE-TIME value. </value>
        /// <remarks>
        /// Within the "VALARM" calendar component, this property defines when the alarm will trigger. The default value type is DURATION, specifying a relative time for the trigger of the alarm. The default duration is relative to the start of an event or to-do that the alarm is associated with. The duration can be explicitly set to trigger from either the end or the start of the associated event or to-do with the "RELATED" parameter. A value of START will set the alarm to trigger off the start of the associated event or to-do. A value of END will set the alarm to trigger off the end of the associated event or to-do.
        /// </remarks>
        /// <seealso cref="Duration"/>
        public object Trigger
        {
            get { return trigger; }
            set
            {
                SecUtility.VerifyDurationDateTime(value, "trigger");
                trigger = value;
            }
        }

        public Duration Duration { get; set; }
        public ICalendar.CalendarProperties.Attachment Attachment { get; set; }
        public Attendee Attendee { get; set; }

       //public Alarm()
       // {

       // }
          
        public Alarm(ActionValue actionValue, object trigger)
        {
            SecUtility.VerifyDurationDateTime(trigger, "trigger");
            this.Action = new CalendarProperties.Action();
            this.TriggerRelationship = TriggerRelationship.Start;
            this.Action.Value = actionValue;
            this.Trigger = trigger;
            this.Duration = new Duration();
        }

       public override string ToString()
        {
            var strAlarm = new StringBuilder();
            
            // Event header
            strAlarm.Append(Util.Write("BEGIN", "VALARM"));

           //TRIGGER
            strAlarm.Append( Util.GetTriggerString(Trigger, TriggerRelationship));

            // ACTION
            strAlarm.Append(this.Action);

            // Title
            strAlarm.Append(Util.Write("SUMMARY", this.Summary));

            // Description
            strAlarm.Append(Util.Write("DESCRIPTION", this.Description));

            // Repeat
            strAlarm.Append(Util.Write("REPEAT", Repeat > 0 ? this.Repeat.ToString() : null));

            // Description
            strAlarm.Append(this.Attendee);
            
            // ATTACH
            strAlarm.Append(Util.Write("ATTACH", this.Attachment));

            //DURATION
            strAlarm.Append(Util.Write("DURATION", this.Duration.ToString()));
            
            // Event footer
            strAlarm.Append(Util.Write("END", "VALARM"));

            return strAlarm.ToString();
        }
    }

}
